﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_System_PostList : System.Web.UI.Page
{
    ISys_PostHandler postHandler = new Sys_PostManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "Id";
            ViewState["Accending"] = "asc";

            dataBind(string.Empty, null);
          
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("203010"))
            {
                if (strPur.Contains("203011"))
                {
                    btnCreate.Visible = true;
                }
                else
                {
                    btnCreate.Visible = false;
                }
            }
            else
            {
                return false;
            }
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 初始化岗位信息表
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {
            SqlParameter[] parms2 = null;
            int counter = 0;
            DataTable dt = new DataTable();

            if (whereClause != string.Empty)
            {
                dt = postHandler.GetSys_PostByPageList(whereClause, parms, ViewState["OrderBy"].ToString() + " " + ViewState["Accending"].ToString(), AspNetPager.StartRecordIndex - 1, AspNetPager.PageSize);
                whereClause = "1=1";
                ProcessParms(ref whereClause, ref parms2);
                counter = postHandler.GetCounter(whereClause, parms2);
            }
            else
            {
                dt = postHandler.GetSys_PostByPageList(ViewState["OrderBy"].ToString() + " " + ViewState["Accending"].ToString(), AspNetPager.StartRecordIndex - 1, AspNetPager.PageSize);
                counter = postHandler.GetCounter();
            }

            this.AspNetPager.RecordCount = counter;            
            gvPostList.DataSource = dt;
            gvPostList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }

    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();

        if (txtName.Text != string.Empty)
        {
            whereClause += " and Name like + '%' + @Name + '%'";
            lstParm.Add(new SqlParameter("@Name", txtName.Text.Trim()));
        }

        parms = lstParm.ToArray();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SqlParameter[] parms = null;
        string clause = " 1=1 ";
        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }

    protected void gvPostList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //
        if (e.CommandName == "del")
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString().Trim());
            try
            {
                postHandler.DeleteSys_Post(id);
                Jscript.AlertInAjax("操作成功!", UpdatePanel1);
                SqlParameter[] parms = null;
                string clause = " 1=1 ";
                ProcessParms(ref clause, ref parms);
                dataBind(clause, parms);
            }
            catch
            {
                Jscript.AlertInAjax("操作失败!", UpdatePanel1);
            }
        }
        else if (e.CommandName == "edt")
        {
            SqlParameter[] parms = null;
            string clause = " 1=1 ";
            ProcessParms(ref clause, ref parms);
            dataBind(clause, parms);
        }
    }

    protected void gvPostList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibtnEdit = (ImageButton)e.Row.FindControl("ibtnSelect");
            ImageButton ibtnDel = (ImageButton)e.Row.FindControl("ibtnDelete");
            if (Session["UserPurviews"] != null)
            {
                if (Session["UserPurviews"].ToString().Contains("203012"))//修改
                {
                    ibtnEdit.Visible = true;
                }
                else
                {
                    ibtnEdit.Visible = false;
                }

                if (Session["UserPurviews"].ToString().Contains("203013"))//删除
                {
                    ibtnDel.Visible = true;
                }
                else
                {
                    ibtnDel.Visible = false;
                }
            }

            if (e.Row.RowIndex > -1)
            {
                e.Row.Cells[3].Text = Enums.GetNormalStatusName((Enums.NormalStatus)int.Parse(e.Row.Cells[3].Text));
            }
        }
    }

    protected void gvPostList_Sorting(object sender, GridViewSortEventArgs e)
    {

        if (e.SortExpression.ToLower() == ViewState["OrderBy"].ToString().ToLower())
        {
            if (ViewState["Accending"].ToString().ToLower() == "asc")
            {
                ViewState["Accending"] = "desc";
            }
            else
            {
                ViewState["Accending"] = "asc";
            }
        }
        else
        {
            ViewState["OrderBy"] = e.SortExpression;
        }

        SqlParameter[] parms = null;
        string clause = " 1=1 ";
        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }
    protected void CreateButton_Click(object sender, EventArgs e)
    {
        SqlParameter[] parms = null;
        string clause = " 1=1 ";
        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }
    protected void AspNetPager_PageChanged(object src, EventArgs e)
    {
        SqlParameter[] parms = null;
        string clause = " 1=1 ";
        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }
}
